package ui;

import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;

import download.Download;

public class TableModel extends AbstractTableModel
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5176625438547898880L;
	private ArrayList<Download> downloads = new ArrayList<Download>();
	private ArrayList<JButton> buttons = new ArrayList<JButton>();
	private String[] cols;
	
	public TableModel(String[] cols)
	{
		this.cols = cols;
	}
	
	public void remove(int row)
	{
		downloads.remove(row);
		buttons.remove(row);
	}
	public void remove(Download dl)
	{
		buttons.remove(downloads.indexOf(dl));
		downloads.remove(dl);
	}
	public void action(int row)
	{
		Download dl = downloads.get(row);
		if(dl.isStopped()&&!dl.isFinished())
		{
			dl.start();
			buttons.get(row).setText("Stop");
		}
		else if(!dl.isStopped()&&!dl.isFinished())
		{
			dl.setStopped(true);
			buttons.get(row).setText("Go");
		}
		else
			buttons.get(row).setEnabled(false);
	}
	public Download getRowAt(int row)
	{
		return downloads.get(row);
	}
	public int getButtonRow(JButton b)
	{
		return buttons.indexOf(b);
	}
	
	public void startAll()
	{
		for(int i = 0; i < downloads.size(); i++)
		{
			Download dl = downloads.get(i);
			if(dl.isStopped()&&!dl.isFinished())
			{
				dl.start();
				updateAction(i);
			}
		}
	}
	public void stopAll()
	{
		for(int i = 0; i < downloads.size(); i++)
		{
			Download dl = downloads.get(i);
			if(!dl.isStopped()&&!dl.isFinished())
			{
				dl.setStopped(true);
				updateAction(i);
			}
		}
	}
	private void updateAction(int row)
	{
		if(getRowAt(row).isStopped())
		{
			buttons.get(row).setText("Go");
		}
		else
			buttons.get(row).setText("Stop");
	}
	@Override
	public Object getValueAt(int row, int col)
	{
		Download dl = downloads.get(row);
		JButton b = buttons.get(row);
		updateAction(row);
		if(col == 0)
			return dl.getUrl().toString();
		else if(col == 1)
			return (Float)dl.getProgress();
		else if(col == 2)
			return (Double)dl.getSpeed();
		else if(col == 3)
			return b;
		else if(col == 4)
		{
			return dl.getSize();
		}
		else
			return null;
		
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Class getColumnClass(int col)
	{
		if(col == 0)
			return String.class;
		else if(col == 1)
			return Float.class;
		else if(col == 2)
			return Double.class;
		else if(col == 3)
			return JButton.class;
		else if(col == 4)
			return Integer.class;
		
		return null;
	}
	
	@Override
	public int getRowCount()
	{
		return downloads.size();
	}
	
	
	public void addRow(Download dl)
	{
		downloads.add(dl);
		JButton b = new JButton("Go");
		buttons.add(b);
		
	}
	

	@Override
	public int getColumnCount()
	{
		return cols.length;
	}

	public void setColumnIdentifiers(String[] cols)
	{
		this.cols = cols;
		
	}
	
	@Override
	public String getColumnName(int col)
	{
		return cols[col];
	}
	
	

}
